package com.situ.mall.service.impl;

import com.situ.mall.mapper.CartMapper;
import com.situ.mall.pojo.Cart;
import com.situ.mall.pojo.vo.CartVO;
import com.situ.mall.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CartServiceImpl implements ICartService {
    @Autowired
    private CartMapper cartMapper;

    @Override
    public void inert(Cart cart) {
        // 首先根据product_id和user_id看购物车里面有没有
        int count = cartMapper.checkExist(cart.getUserId(), cart.getProductId());
        if (count > 0) {
            // 这个用户user_id的购物车里面已经有这个商品product_id，更新一下数量
            cartMapper.updateQuantityByUserIdAndProdcutId(cart);
        } else {
            // 如果没有再做插入
            cartMapper.insert(cart);
        }
    }

    @Override
    public List<CartVO> selectByUserId(Integer id) {
        return cartMapper.selectByUserId(id);
    }

    @Override
    public void updateChecked(Integer id, Integer checked) {
        cartMapper.updateChecked(id, checked);
    }

    @Override
    public List<CartVO> selectByUserIdAndChecked(Integer id) {
        return cartMapper.selectByUserIdAndChecked(id);
    }
}
